package com.lanou3g.demo.service.admininfo;

import com.lanou3g.demo.entity.AdminInfo;
import com.lanou3g.demo.entity.AdminRole;
import com.lanou3g.demo.entity.ex.AdminWithRole;
import com.lanou3g.demo.mapper.AdminInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员模块Service
 */
@Service
public class AdminInfoService implements IAdminInfoService {

    private static AdminInfoMapper adminInfoMapper;


    @Autowired
    public AdminInfoService(AdminInfoMapper adminInfoMapper) {
        AdminInfoService.adminInfoMapper = adminInfoMapper;
    }

    /**
     * 管理员表有多少条数据
     *
     * @return
     */
    @Override
    public int totalCount() {
        return adminInfoMapper.totalCount();
    }

    // 根据id查询管理员信息
    @Override
    public AdminInfo findOneByAdminId(Integer id) {
        return adminInfoMapper.findOneByAdminId(id);
    }


    /**
     * 查询所有的管理员和管理员所对应的角色
     *
     * @param page  铺页面时的第几页
     * @param count 铺页面时的每页显示的条数
     * @return
     */
    @Override
    public List<AdminWithRole> findAll(Integer page, Integer count, String searchMsg) {

        Map<String, Object> map = new HashMap<>();
        page = (page - 1) * count;
        map.put("page", page);
        map.put("count", count);
        if (searchMsg != null && !searchMsg.equals("")) {
            map.put("search", searchMsg.trim());
            System.out.println("Service:" + searchMsg);
        }
        return adminInfoMapper.findAll(map);
    }

    /**
     * 根据id删除角色
     *
     * @param adminId
     * @return
     */
    @Override
    @Transactional
    public int deleteAdmin(Integer adminId) {
        int i = adminInfoMapper.deleteAdmin(adminId);
        adminInfoMapper.deleteAdminRole(adminId);

        return i;
    }

    /**
     *  更新一个管理员的信息
     * @param adminInfo Controller层传递过来的管理员对象
     * @param allRole   这个管理员对象对应的所有角色
     * @return
     */
    @Override
    @Transactional
    public int updateAdmin(AdminInfo adminInfo,String allRole) {

        int i = 0;

        Integer adminId = adminInfo.getAdminId();
        String name = adminInfo.getAdminName();
        String telephone = adminInfo.getTelephone();
        String email = adminInfo.getEmail();

        AdminInfo byAdminInfo = adminInfoMapper.findOneByAdminId(adminInfo.getAdminId());
        Map<String,Object> map = new HashMap<>();
        map.put("adminId",adminId);
        if (!byAdminInfo.getAdminName().equals(name)){
            map.put("name",name);
        }
        if (!byAdminInfo.getTelephone().equals(telephone)){
            map.put("telephone",telephone);
        }
        if (!byAdminInfo.getEmail().equals(email)){
            map.put("email",email);
        }
        if (map.size() != 1){
            i = adminInfoMapper.updateAdmin(map);
        }

        adminInfoMapper.deleteAdminRole(adminId);

        if (allRole != null){

            addAdminRole(adminId,allRole);
        }

        return i;
    }


    @Override
    @Transactional
    public int addOneAdmin(AdminInfo adminInfo,String allRole) {
        int i = adminInfoMapper.addOneAdmin(adminInfo);
        Integer adminId = adminInfo.getAdminId();
        if (allRole != null){
            addAdminRole(adminId,allRole);
        }
        return i;
    }

    @Override
    @Transactional
    public int addOneRole(AdminRole adminRole) {
        int i = adminInfoMapper.addOneRole(adminRole);
        return  i;
    }



    public static void addAdminRole(Integer adminId, String str){
        String[] strings = str.split(",");
        if ("100".equals(strings[0])) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(100);
            adminInfoMapper.addOneRole(adminRole);
        }else{
            for (String s : strings) {
                AdminRole adminRole = new AdminRole();
                adminRole.setAdminId(adminId);
                adminRole.setRoleId(Integer.parseInt(s));
                adminInfoMapper.addOneRole(adminRole);
            }

        }
    }


}
